package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.U_article;

import cn.com.api.service.U_articleService;

import cn.com.util.*;


public class U_articleBaseController {

	private static Logger logger = Logger.getLogger(U_articleBaseController.class);
	
	@Autowired
	private U_articleService u_articleService;
	
	/**
	 * 列表
	 * @return
	 */
	@RequestMapping(value = "u_article-list")
	public ModelAndView u_article_list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/u_article_list.jsp");
		
		List<U_article> u_articles = u_articleService.findU_articles();
		mv.addObject("u_articles", u_articles);
		
		return mv;
	}
	
	/**
	 * 进入新增或者编辑页面
	 * @return
	 */
	@RequestMapping(value = "u_article-add-pre")
	public ModelAndView u_article_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/u_article_add.jsp");
		//
		if(request.getParameter("id")!=null){
			String id = request.getParameter("id");
			U_article u_article = u_articleService.findU_articleById(Integer.valueOf(id));
			mv.addObject("u_article",u_article);
		}
		return mv;
	}

	/**
	 *新增或者编辑
	 * @return
	 */
	@RequestMapping(value = "u_article-add")
	public void u_article_add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String cate_id = request.getParameter("cate_id");
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		String author = request.getParameter("author");
		String atuthor_email = request.getParameter("atuthor_email");
		String sort = request.getParameter("sort");
		String keywords = request.getParameter("keywords");
		String article_type = request.getParameter("article_type");
		String is_visiable = request.getParameter("is_visiable");
		String add_time = request.getParameter("add_time");
		String read_count = request.getParameter("read_count");
		String description = request.getParameter("description");
		String header_img = request.getParameter("header_img");
		String img1 = request.getParameter("img1");
		String img2 = request.getParameter("img2");
		String img3 = request.getParameter("img3");
		String label = request.getParameter("label");
		String creater_id = request.getParameter("creater_id");
		String modified_by = request.getParameter("modified_by");
		String modified_time = request.getParameter("modified_time");
		String share_count = request.getParameter("share_count");
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		//add
		if(id==null || (id.trim()).equals("")){
			U_article u_article = new U_article();
            /*
		    u_article.setId(id);
		    u_article.setCate_id(cate_id);
		    u_article.setTitle(title);
		    u_article.setContent(content);
		    u_article.setAuthor(author);
		    u_article.setAtuthor_email(atuthor_email);
		    u_article.setSort(sort);
		    u_article.setKeywords(keywords);
		    u_article.setArticle_type(article_type);
		    u_article.setIs_visiable(is_visiable);
		    u_article.setAdd_time(add_time);
		    u_article.setRead_count(read_count);
		    u_article.setDescription(description);
		    u_article.setHeader_img(header_img);
		    u_article.setImg1(img1);
		    u_article.setImg2(img2);
		    u_article.setImg3(img3);
		    u_article.setLabel(label);
		    u_article.setCreater_id(creater_id);
		    u_article.setModified_by(modified_by);
		    u_article.setModified_time(modified_time);
		    u_article.setShare_count(share_count);
	        */
			u_articleService.insertU_article(u_article);
		    //
            map.put(Util.RESULT, Util.RESULT_RC_SUCCESS);
		    map.put(Util.MESSAGE, "添加成功！");
        }
		//edit
		else{
			U_article u_article = u_articleService.findU_articleById(Integer.valueOf(id));
	        /*
		    u_article.setCate_id(cate_id);
		    u_article.setTitle(title);
		    u_article.setContent(content);
		    u_article.setAuthor(author);
		    u_article.setAtuthor_email(atuthor_email);
		    u_article.setSort(sort);
		    u_article.setKeywords(keywords);
		    u_article.setArticle_type(article_type);
		    u_article.setIs_visiable(is_visiable);
		    u_article.setAdd_time(add_time);
		    u_article.setRead_count(read_count);
		    u_article.setDescription(description);
		    u_article.setHeader_img(header_img);
		    u_article.setImg1(img1);
		    u_article.setImg2(img2);
		    u_article.setImg3(img3);
		    u_article.setLabel(label);
		    u_article.setCreater_id(creater_id);
		    u_article.setModified_by(modified_by);
		    u_article.setModified_time(modified_time);
		    u_article.setShare_count(share_count);
			*/
			u_articleService.updateU_article(u_article);
		    //
            map.put(Util.RESULT, Util.RESULT_RC_SUCCESS);
		    map.put(Util.MESSAGE, "修改成功！");
        }
		//
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
	/**
	 *删除
	 * @return
	 */
	@RequestMapping(value = "u_article-delete")
	public void u_article_delete(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		
		//delete
		int ret_i =	u_articleService.deleteU_article(Integer.valueOf(id));
		
		//
		if(ret_i > 0){
			map.put(Util.RESULT, Util.RESULT_RC_SUCCESS);
			map.put(Util.MESSAGE, "删除成功！");
		}

		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
}



